
from __future__ import unicode_literals
from django.shortcuts import render
import os
import math

# Create your views here.
from django.core.paginator import Paginator
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from django.template import loader

from .models import *
import pyecharts.options as opts
from pyecharts.charts import Pie


# Create your views here.

datalist = ['CPU使用率','内存使用率','磁盘使用率','通道使用数']
# 参数柱状图 

def resourceShow(request):
    template =loader.get_template('admin/admin_test.html')


def get_pie():
	x_data = ["CPU使用率", "CPU使用率", "内存使用率", "内存使用率", "内存使用率"]
	y_data = [335, 310, 274, 235, 400]
	data_pair = [list(z) for z in zip(x_data, y_data)]
	data_pair.sort(key=lambda x: x[1])
	return (
		Pie(init_opts=opts.InitOpts(width="1600px", height="800px", bg_color="#2c343c"))
		.add(
			series_name="CPU使用率",
			data_pair=data_pair,
			rosetype="radius",
			radius="55%",
			center=["50%", "50%"],
			label_opts=opts.LabelOpts(is_show=False, position="center"),
		)
		.set_global_opts(
			title_opts=opts.TitleOpts(
				title="Customized Pie",
				pos_left="center",
				pos_top="20",
				title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
			),
			legend_opts=opts.LegendOpts(is_show=False),
		)
		.set_series_opts(
			tooltip_opts=opts.TooltipOpts(
				trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
			),
			label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
		)
		.render("customized_pie.html")
	)


def every_para_list(datalist):
	para_list =[]
	#for i in datalist:
	#str = datalist(i)


'''
@login_required(login_url='/login')
def linux_monitor(request):
  messageinfo_list = models.TabAlarmInfo.objects.all()
  tagsdefault = request.GET.get('tagsdefault')
  if not tagsdefault:
    tagsdefault = models.TabLinuxServers.objects.order_by('tags')[0].tags
  cpu_range_defualt =  request.GET.get('cpu_range_default')
  if not cpu_range_defualt:
    cpu_range_defualt = '1小时'.decode("utf-8")
  mem_range_default = request.GET.get('mem_range_default')
  if not mem_range_default:
    mem_range_default = '1小时'.decode("utf-8")
  net_range_default = request.GET.get('net_range_default')
  if not net_range_default:
    net_range_default = '1小时'.decode("utf-8")
  hostinfo = models.TabLinuxServers.objects.all().order_by('tags')
  
  net_begin_time = tools.range(net_range_default)
  cpu_begin_time = tools.range(cpu_range_defualt)
  mem_begin_time = tools.range(mem_range_default)
  end_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  
  netgrow = models.OsInfoHis.objects.filter(tags=tagsdefault, recv_kbps__isnull=False).filter(
    chk_time__gt=net_begin_time, chk_time__lt=end_time).order_by('-chk_time')
  netgrow_list = list(netgrow)
  netgrow_list.reverse()
  
  cpugrow = models.OsInfoHis.objects.filter(tags=tagsdefault, cpu_used__isnull=False).filter(
    chk_time__gt=cpu_begin_time, chk_time__lt=end_time).order_by('-chk_time')
  cpugrow_list = list(cpugrow)
  cpugrow_list.reverse()
  
  memgrow = models.OsInfoHis.objects.filter(tags=tagsdefault, mem_used__isnull=False).filter(
    chk_time__gt=mem_begin_time, chk_time__lt=end_time).order_by('-chk_time')
  memgrow_list = list(memgrow)
  memgrow_list.reverse()
  
  diskinfos = models.OsFilesystem.objects.filter(tags=tagsdefault)
  
  try:
    osinfo = models.OsInfo.objects.get(tags=tagsdefault)
  except models.OsInfo.DoesNotExist:
    osinfo = models.OsInfoHis.objects.filter(tags=tagsdefault,cpu_used__isnull=False).order_by('-chk_time')[0]
  
  if request.method == 'POST':
    if request.POST.has_key('select_tags') or request.POST.has_key('select_cpu')or request.POST.has_key('select_mem') or request.POST.has_key('select_net'):
      if request.POST.has_key('select_tags'):
        tagsdefault = request.POST.get('select_tags', None).encode("utf-8")
      elif request.POST.has_key('select_net'):
        net_range_defualt = request.POST.get('select_net',None)
      elif request.POST.has_key('select_cpu'):
        cpu_range_defualt = request.POST.get('select_cpu',None)
      elif request.POST.has_key('select_mem'):
        mem_range_default = request.POST.get('select_mem', None)
      return HttpResponseRedirect('/linux_monitor?tagsdefault=%s&net_range_default=%s&cpu_range_default=%s&mem_range_default=%s' %(tagsdefault,net_range_default,cpu_range_defualt,mem_range_default))
  
    else:
      logout(request)
      return HttpResponseRedirect('/login/')
  
  if messageinfo_list:
    msg_num = len(messageinfo_list)
    msg_last = models.TabAlarmInfo.objects.latest('id')
    msg_last_content = msg_last.alarm_content
    tim_last = (datetime.datetime.now() - msg_last.alarm_time).seconds / 60
    return render_to_response('linux_monitor.html', {'netgrow_list':netgrow_list,'cpugrow_list':cpugrow_list,'memgrow_list':memgrow_list, 'tagsdefault':tagsdefault, 'hostinfo':hostinfo, 'osinfo': osinfo,'net_range_default':net_range_default,'cpu_range_default':cpu_range_defualt,'mem_range_default':mem_range_default, 'messageinfo_list': messageinfo_list,
                          'msg_num': msg_num,'msg_last_content': msg_last_content, 'tim_last': tim_last,'diskinfos':diskinfos})
  else:
    return render_to_response('linux_monitor.html', {'netgrow_list':netgrow_list,'cpugrow_list':cpugrow_list,'memgrow_list':memgrow_list, 'tagsdefault':tagsdefault, 'hostinfo':hostinfo, 'osinfo': osinfo,'net_range_default':net_range_default,'cpu_range_default':cpu_range_defualt,'mem_range_default':mem_range_default,'diskinfos':diskinfos})

'''

